-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Misc typing fixes in not annotated functions #6389
Conversation
@@ -39,7 +39,7 @@ class _Setting: | |||
"""The properties of a setting and how to read it.""" | |||
|
|||
name: str | |||
read_from: str | None = None | |||
read_from: str = None # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use this pattern, allowing None in a dataclass field to provide a default in __post_init__
Ignore the type here to avoid multiple issues about it being nullable all over the place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably just remove the read_from
feature, it's only used once
lms/models/application_instance.py
Outdated
@@ -92,7 +92,7 @@ class ApplicationInstance(CreatedUpdatedMixin, Base): | |||
|
|||
consumer_key = sa.Column(sa.Unicode, unique=True, nullable=True) | |||
shared_secret = sa.Column(sa.Unicode, nullable=False) | |||
lms_url = sa.Column(sa.Unicode(2048), nullable=False) | |||
lms_url: Mapped[str] = mapped_column(sa.Unicode(2048), nullable=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably automate a conversion to this style for all models. Doing only the ones that solve issues in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think nullable=False
is the default when using Mapped[str]
. (If you want it to be nullable you have to do Mapped[str | None]
)
from lms.services.exceptions import CanvasAPIPermissionError, FileNotFoundInCourse | ||
|
||
|
||
class CanvasService: | ||
"""A high level Canvas service.""" | ||
|
||
api = None | ||
api: CanvasAPIClient = None # type:ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT this is only here for the tests benefict. Ignore this declaration so all the code below assumes the right type.
@@ -43,7 +43,7 @@ def get_by_id(self, id_: int) -> Organization | None: | |||
|
|||
return self._organization_search_query(id_=id_).one_or_none() | |||
|
|||
def get_by_public_id(self, public_id: str) -> list | None: | |||
def get_by_public_id(self, public_id: str) -> Organization | None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was wrong
b92c019
to
678c29a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a quick test of dumping the DB schema from a DB created on main
and from one created on this branch diffing them, the only difference is the change in the type of the deployment_id
column which I noted. Probably a good idea to do this test whenever changing the models
@@ -39,7 +39,7 @@ class _Setting: | |||
"""The properties of a setting and how to read it.""" | |||
|
|||
name: str | |||
read_from: str | None = None | |||
read_from: str = None # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably just remove the read_from
feature, it's only used once
"""Return a SA column type to store the python enum.Enum as a varchar in a table.""" | ||
return sa.Column( | ||
return mapped_column( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is probably a pretty good example of SQLAlchemy cleverness that we'd be better off without. If we want to add Python-only validation to a column, SQLAlchemy has a simple way to do that: https://docs.sqlalchemy.org/en/20/orm/mapped_attributes.html#simple-validators
lms/models/application_instance.py
Outdated
@@ -92,7 +92,7 @@ class ApplicationInstance(CreatedUpdatedMixin, Base): | |||
|
|||
consumer_key = sa.Column(sa.Unicode, unique=True, nullable=True) | |||
shared_secret = sa.Column(sa.Unicode, nullable=False) | |||
lms_url = sa.Column(sa.Unicode(2048), nullable=False) | |||
lms_url: Mapped[str] = mapped_column(sa.Unicode(2048), nullable=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think nullable=False
is the default when using Mapped[str]
. (If you want it to be nullable you have to do Mapped[str | None]
)
lms/models/lti_role.py
Outdated
@@ -39,27 +40,25 @@ class LTIRoleOverride(Base): | |||
|
|||
id = sa.Column(sa.Integer(), autoincrement=True, primary_key=True) | |||
|
|||
lti_role_id = sa.Column( | |||
sa.Integer(), | |||
lti_role_id: Mapped[int] = mapped_column( | |||
sa.ForeignKey("lti_role.id", ondelete="cascade"), | |||
nullable=True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this has nullable=True
shouldn't it be Mapped[int | None]
? (Then you wouldn't need the nullable=True
)
lms/models/lti_role.py
Outdated
|
||
application_instance_id = sa.Column( | ||
sa.Integer(), | ||
application_instance_id: Mapped[int] = mapped_column( | ||
sa.ForeignKey("application_instances.id", ondelete="cascade"), | ||
nullable=False, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think nullable=False
is the default with Mapped
lms/models/application_instance.py
Outdated
) | ||
|
||
lti_registration = sa.orm.relationship( | ||
"LTIRegistration", back_populates="application_instances" | ||
) | ||
|
||
# Unique identifier of this instance per LTIRegistration | ||
deployment_id = sa.Column(sa.UnicodeText, nullable=True) | ||
deployment_id: Mapped[str | None] = mapped_column() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the type of the column from text
to character varying
Quick fixes with the goal of enabling mypy's check_untyped_defs
678c29a
to
ebbe824
Compare
Quick fixes with the goal of enabling mypy's check_untyped_defs
First round of fixes.